<%--
  Created by IntelliJ IDEA.
  User: 86187
  Date: 2021/10/21
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表</title>
    <link href="/resource/css/bootstrap.css" rel="stylesheet">
    <script type="text/javascript" src="/resource/js/jquery-3.2.1.js"></script>

</head>
<body>
<h3>商品列表</h3>
<div class="text-right">登录人：${user.username},<a href="/logout">注销</a></div>
<div>
    <form action="list" method="post">
      商品名称：<input type="text" name="name" value="${goodsVO.name}">
        商品价格：<input type="text" name="priceStart" value="${goodsVO.priceStart}">--
        <input type="text" name="priceEnd" value="${goodsVO.priceEnd}">
        品牌：
        <select name="bid" id="bid">
            <option value="">所有</option>
          <%--  <c:forEach items="${listBrands}" var="brand">

                <option value="${brand.bid}" ${brand.bid==goodsVO.bid?"selected":""}>${brand.bname}</option>
            </c:forEach>--%>
        </select>
        <button type="submit">查询</button>
        <button type="button" onclick="deleteBatch()">批量删除</button>
    </form>

</div>
<table class="table">
    <tr>
        <td>
            <button type="button" onclick="chkAll(1)" >全选</button>
            <button type="button" onclick="chkAll(2)" >全不选</button>
            <button type="button" onclick="chkAll(3)" >反选</button>
        </td>
        <td>商品id</td>
        <td>商品名称</td>
        <td>商品价格</td>
        <td>商品描述</td>
        <td>所属品牌</td>
        <td>所属分类1</td>
        <td>所属分类2</td>
        <td>所属分类3</td>
        <td>商品图片</td>
        <td>商品状态</td>
        <td>创建日期</td>
        <td>操作<a href="/add">添加</a></td>
    </tr>

    <c:forEach items="${info.list}" var="goods">
        <tr>
            <td><input name="gid" type="checkbox" value="${goods.id}"></td>
            <td>${goods.id}</td>
            <td>${goods.name}</td>
            <td>${goods.price}</td>
            <td>${goods.ms}</td>
            <td>${goods.bname}</td>
            <td>${goods.typename1}</td>
            <td>${goods.typename2}</td>
            <td>${goods.typename3}</td>
            <td>
                <!--delims 按， 分割字符串 items-->
                <c:forTokens items="${goods.picurl}" delims="," var="picurl" begin="0" end="0">
                    <img src="${picurl}" width="60px" height="45px">

                </c:forTokens>

              </td>
            <td>${goods.status==0?"上架":"下架"}</td>
            <td><fmt:formatDate value="${goods.created}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate> </td>
            <td><a href="update?id=${goods.id}"> 修改 </a>|<a href="javascript:deleteById(${goods.id})">删除</a></td>
        </tr>
    </c:forEach>

</table>

  <div>
    <%--  <a href="/list?pageNum=1">首页</a>
      <a href="/list?pageNum=${info.pageNum==1?1:info.pageNum-1}">上一页</a>
      <a href="/list?pageNum=${info.pageNum==info.pages?info.pages:info.pageNum+1}">下一页</a>
      <a href="/list?pageNum=${info.pages}">尾页</a>
      共${info.pages}页/共${info.total}条 当前页${info.pageNum}--%>
      <jsp:include page="/resource/common/pages.jsp"></jsp:include>
  </div>
<script>
    function deleteById(id){
        if(confirm("确定删除吗？")){
            $.post("/deleteById",{id:id},function (flag){
                if(flag){
                    alert("删除成功");
                    location.reload();
                }else{
                    alert("删除失败");
                }
            })
        }
    }
 //全选、反选 全不选
    function chkAll(flag){
        if(flag==1){//全选
         $("[name='gid']").prop("checked",true);
        }else if(flag==2){//全不选
            $("[name='gid']").prop("checked",false);
        }else{//反选
            $("[name='gid']").each(function (){
                this.checked=!this.checked;
            })
        }
    }
    //执行批量删除
function deleteBatch(){
   var gids=new Array();
    $("[name='gid']:checked").each(function(i){
        gids[i]=this.value;//把选中的值放入数组
    })
   if(gids==null ||gids.length==0){
       alert("请至少选中一个删除")
       return ;
   }
   $.post("/deleteBatch",{gids:gids},function (flag){
       if(flag){
           alert("删除成功");
           location.reload();
       }else{
           alert("删除失败");
       }
   })

}


    //动态的获取所有品牌
    $(function (){
        $.get("/listBrand",function (list){
            for(var i in list){
                $("#bid").append("<option value='"+list[i].bid+"'>"+list[i].bname+"</option>")

            }
            //让下拉框默认选中提交选中的值
            var bid= '${goodsVO.bid}';
            $("#bid").val(bid);//选中
        })
    })
</script>
</body>
</html>
